package com.zzh.tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class No199_rightSideView {
    public static void main(String[] args) {
        No199Solution solution = new No199Solution();
        List<Integer> list = solution.rightSideView(TreeNode.initTree());
        System.out.println(list.toString());
    }
}
class No199Solution{
    public List<Integer> rightSideView(TreeNode root) {
        if (root==null){
            return new ArrayList<>();
        }
        Queue<TreeNode> q = new LinkedList<>();
        List<Integer> list = new ArrayList<>();
        q.offer(root);
        while (!q.isEmpty()){
            int size= q.size();
            for (int i=0;i<size;i++){
                TreeNode cur= q.poll();
                if (i==size-1){
                    list.add(cur.val);
                }
                if (cur.left!=null){
                    q.offer(cur.left);
                }
                if (cur.right!=null){
                    q.offer(cur.right);
                }
            }
        }
        return list;
    }

}
